﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using System.Data.SqlServerCe;

namespace Highlighterr
{
    public partial class RenameForm : Form
    {

        private Intellisenser _Intellisenser = null;
        private Document _Document = null;
        private TextSelection _Selection = null;
        private string _OrigString = string.Empty;
        private int _OrigTVHeight = 0;

        public RenameForm(Intellisenser i, Document d)
        {
            InitializeComponent();

            _Intellisenser = i;
            _Document = d;

            _Selection = d.Selection;
            _Selection.WordLeft();
            _Selection.WordRight(true);
            _OrigString = _Selection.Text.Trim();
            NewNameTextBox.Text = _OrigString;

            FindDefinition();
        }


        private void FindDefinition()
        {
            //StringBuilder sb = new StringBuilder();
            //int cmds = _Intellisenser.Dte2.Commands.Count;
            //for (int i = 1; i <= cmds; i++)
            //{
            //    Command cmd = _Intellisenser.Dte2.Commands.Item(i);
            //    sb.Append(cmd.Name);
            //    sb.Append("\n");
            //}
            //string s = sb.ToString();

            //_Intellisenser.Dte2.ExecuteCommand("Edit.FindSymbol", _OrigString);

            try
            {

                ResultsTreeView.Nodes.Clear();

                SqlCeConnection cn = new SqlCeConnection(_Intellisenser.SDFConnectionString);
                cn.Open();
                SqlCeCommand comm = cn.CreateCommand();
                string sql = @"select 
                            code_items.id, 
                            code_items.parent_id, 
                            files.name, 
                            code_items.kind, 
                            code_item_kinds.name, 
                            code_items.attributes 
                        from code_items 
                            inner join files 
                                on code_items.file_id = files.id 
                            inner join code_item_kinds 
                                on code_items.kind = code_item_kinds.id 
                        where code_items.name='" + _OrigString.Replace("'", "''") + "'";
                comm.CommandText = sql;
                SqlCeDataReader rdr = comm.ExecuteReader();

                int rowcount = 0;
                while (rdr.Read())
                {
                    long id = rdr.GetInt64(0);
                    long parentid = rdr.GetInt64(1);
                    string file = rdr.GetString(2);
                    int kind = rdr.GetInt16(3);
                    string skind = rdr.GetString(4);
                    int attr = rdr.GetInt32(5);

                    string fname = System.IO.Path.GetFileName(file);

                    TreeNode tn = new TreeNode(string.Format("id:{0}, file:{1}, parent:{2}, kind:{3}, attributes:{4}", id, fname, parentid, skind, attr));
                    ResultsTreeView.Nodes.Add(tn);

                    rowcount++;
                }

                rdr.Close();
                rdr.Dispose();
                comm.Dispose();
                cn.Close();
                cn.Dispose();


                if (rowcount == 0)
                {
                    MessageBox.Show("Definition not found for \"" + _OrigString + "\"!");
                    Close();
                }
            }
            catch (Exception ex)
            {
                if (_Intellisenser != null)
                {
                    _Intellisenser.LogError(ex.ToString());
                }
            }
        }


        private void RenameOkButton_Click(object sender, EventArgs e)
        {

        }
        private void RenameCancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void SearchCommentsCheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void SearchStringsCheckBox_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void PreviewChangesCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (PreviewChangesCheckBox.Checked)
            {
                ResultsTreeView.Visible = true;
                Height += _OrigTVHeight;
            }
            else
            {
                _OrigTVHeight = ResultsTreeView.Height;
                ResultsTreeView.Visible = false;
                Height -= ResultsTreeView.Height;
            }
        }




    }
}
